package com.xzz.finger;

/**
 * @author: hhz
 * @create: 2022-02-18 14:46
 * 剑指 Offer 26. 树的子结构
 **/
public class IsSubStructure {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public static void main(String[] args) {

    }

    public static boolean isSubStructure(TreeNode A, TreeNode B) {
        return (A!=null && B!=null) && (judge(A,B) || isSubStructure(A.left,B) || isSubStructure(A.right,B));
    }

    public static boolean judge(TreeNode A, TreeNode B) {
        if(B ==null){
            return true;
        }
        if(A==null || A.val !=B.val){
            return false;
        }
        return judge(A.left,B.left) && judge(A.right,B.right);
    }
}
